//
//  PayTableViewCell.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/26.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit
// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

// FIXME: comparison operators with optionals were removed from the Swift Standard Libary.
// Consider refactoring the code to use the non-optional operators.
fileprivate func >= <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l >= r
  default:
    return !(lhs < rhs)
  }
}


protocol PayTableViewCellDelegate {
    func monthDelegateToPayVC()
    func yearDelegateToPayVC()
}

class PayTableViewCell: UITableViewCell,UITextFieldDelegate {

    var delegate : PayTableViewCellDelegate?
    @IBOutlet weak var CardView: UIView!
    
    @IBOutlet weak var CardTextField: UITextField!
    
    
    @IBOutlet weak var CVCView: UIView!
    
    
    @IBOutlet weak var CVCTextField: UITextField!
    
    
    @IBOutlet weak var monthBtn: UIButton!
    
    
    @IBOutlet weak var yearBtn: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        CardView.layer.borderColor = UIColor.gray.cgColor
        CardView.layer.borderWidth = 1
        
        CVCView.layer.borderColor = UIColor.gray.cgColor
        CVCView.layer.borderWidth = 1
        
        monthBtn.layer.borderColor = UIColor.gray.cgColor
        monthBtn.layer.borderWidth = 1
        if SCREEN_WIDTH==320 {
            monthBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        }else{
            monthBtn.titleLabel?.font = UIFont.systemFont(ofSize: 19)
        }
        
        yearBtn.layer.borderColor = UIColor.gray.cgColor
        yearBtn.layer.borderWidth = 1
        if SCREEN_WIDTH==320 {
            yearBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        }else{
            yearBtn.titleLabel?.font = UIFont.systemFont(ofSize: 19)
        }
        
        CardTextField.borderStyle = .none
        CardTextField.keyboardType = .numberPad
        CVCTextField.borderStyle = .none
        CVCTextField.keyboardType = .numberPad
        
        CardTextField.addTarget(self, action: #selector(limitTextFieldLength), for: .editingChanged)
        CardTextField.delegate = self
        CVCTextField.addTarget(self, action: #selector(minTextFieldLength), for: .editingChanged)
        CVCTextField.delegate = self
        // Initialization code
    }

    
    func limitTextFieldLength(){
        if CardTextField.text?.characters.count >= 16 {

            
//            CardTextField.text = CardTextField.text?.substringWithRange(NSRange.init(location: 0, length: 16))
            CardTextField.resignFirstResponder()
        }
    }
    
    func minTextFieldLength(){
        if CVCTextField.text?.characters.count >= 3 {
            CVCTextField.resignFirstResponder()
        }
    }
    
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if textField == CardTextField {
            
            
            } else {
            
        }
        

        
        return true
    }
    
    @IBAction func monthBtnClick(_ sender: AnyObject) {
        
        self.delegate?.monthDelegateToPayVC()
    }
    
    @IBAction func YearBtnClick(_ sender: AnyObject) {
        
        self.delegate?.yearDelegateToPayVC()
    }
    
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
